/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package workshopdesign.Control;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;
import javax.swing.JOptionPane;
import workshopdesign.Model.Copy;
import workshopdesign.Model.DVD;
import workshopdesign.Model.Loan;
import workshopdesign.Model.Person;

/**
 *
 * @author Dom
 */
public class SaveLoadController implements Serializable {

    private HashMap<Long, Person> person_list;
    private HashMap<Long, Loan> loan_list;
    private HashMap<Long, DVD> dvd_list;
    private HashMap<Long, Copy> copy_list;
    private long person_id;
    private long dvd_id;
    private long copy_id;
    private long loan_id;

    public static void save() {
        FileOutputStream fos = null;
        {
            ObjectOutputStream oos = null;
            try {
                SaveLoadController slc = new SaveLoadController();

                slc.person_list = AddressController.save();
                slc.person_id = AddressController.save_id();

                slc.dvd_list = DVDController.save_dvd();
                slc.dvd_id = DVDController.save_dvd_id();

                slc.copy_list = DVDController.save_copy();
                slc.copy_id = DVDController.save_copy_id();

                slc.loan_list = LoanController.save();
                slc.loan_id = LoanController.save_id();

                fos = new FileOutputStream("workshop.data");
                oos = new ObjectOutputStream(fos);
                oos.writeObject(slc);
            } catch (FileNotFoundException ex) {
                JOptionPane.showMessageDialog(null, "Could not save data!\nFile not found o.O");
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Could not save data!\nIO Exception o.O");
            } finally {
                try {
                    oos.close();
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(null, "Could not close output stream! o.O");
                }
                try {
                    fos.close();
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(null, "Could not close output stream! o.O");
                }
            }
        }

    }

    public static void load() {
        FileInputStream fis = null;
        {
            ObjectInputStream ois = null;
            try {
                fis = new FileInputStream("workshop.data");
                ois = new ObjectInputStream(fis);
                SaveLoadController slc = (SaveLoadController) ois.readObject();
                AddressController.load(slc.person_list, slc.person_id);
                DVDController.load_dvd(slc.dvd_list, slc.dvd_id);
                DVDController.load_copy(slc.copy_list, slc.copy_id);
                LoanController.load(slc.loan_list, slc.loan_id);
            } catch (ClassNotFoundException ex) {
                JOptionPane.showMessageDialog(null,
                        "Could not load data! o.O\n " +
                        "At the first run or after " +
                        "an update this is normal!");
            } catch (FileNotFoundException ex) {
                JOptionPane.showMessageDialog(null,
                        "Could not load data! o.O\n " +
                        "At the first run or after " +
                        "an update this is normal!");
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null,
                        "Could not load data! o.O\n " +
                        "At the first run or after " +
                        "an update this is normal!");
            } finally {
                try {
                    ois.close();
                } catch (Exception e) {
                }
                try {
                    fis.close();
                } catch (Exception e) {
                }
            }
        }
    }
}
